package com.nodecollege.cloud.controller;

import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.annotation.UserLoginAnnotation;
import com.nodecollege.cloud.common.model.ApiDataPower;
import com.nodecollege.cloud.common.model.BindVO;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryRelationVO;
import com.nodecollege.cloud.common.model.po.UpmsMember;
import com.nodecollege.cloud.common.model.po.UpmsOrg;
import com.nodecollege.cloud.common.model.po.UpmsRole;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.OrgMemberService;
import com.nodecollege.cloud.service.OrgService;
import com.nodecollege.cloud.service.RoleOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 组织机构管理
 *
 * @author LC
 * @date 2019/12/11 18:51
 */
@RestController
@UserLoginAnnotation(checkDataPower = true)
@RequestMapping("/org")
public class OrgController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private RoleOrgService roleOrgService;

    @Autowired
    private OrgMemberService orgMemberService;

    /**
     * 查询组织机构树
     *
     * @param upmsOrg
     * @return
     */
    @ApiAnnotation(modularName = "组织机构管理", description = "查询组织机构树")
    @PostMapping("/getOrgTree")
    public NCResult getOrgTree(@RequestBody UpmsOrg upmsOrg) {
        upmsOrg.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        upmsOrg.setOrgId(null);
        return NCResult.ok(orgService.getOrgTree(upmsOrg));
    }

    /**
     * 新增组织机构
     *
     * @param upmsOrg
     * @return
     */
    @ApiAnnotation(modularName = "组织机构管理", description = "新增组织机构")
    @PostMapping("/addOrg")
    public NCResult addOrg(@RequestBody UpmsOrg upmsOrg) {
        upmsOrg.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        orgService.addOrg(upmsOrg);
        return NCResult.ok("添加组织机构成功！");
    }

    /**
     * 更新组织机构
     *
     * @param upmsOrg
     */
    @ApiAnnotation(modularName = "组织机构管理", description = "更新组织机构")
    @PostMapping("/updateOrg")
    public NCResult updateOrg(@RequestBody UpmsOrg upmsOrg) {
        upmsOrg.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        orgService.updateOrg(upmsOrg);
        return NCResult.ok("修改组织机构成功！");
    }

    /**
     * 删除组织机构
     *
     * @param upmsOrg
     */
    @ApiAnnotation(modularName = "组织机构管理", description = "删除组织机构")
    @PostMapping("/delOrg")
    public NCResult delOrg(@RequestBody UpmsOrg upmsOrg) {
        upmsOrg.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        orgService.delOrg(upmsOrg);
        return NCResult.ok("删除组织机构成功！");
    }

    @ApiAnnotation(modularName = "组织机构管理", description = "查询组织机构拥有角色列表")
    @PostMapping("/getRoleListByOrgId")
    public NCResult getRoleListByOrgId(@RequestBody QueryRelationVO<UpmsRole, UpmsOrg> queryVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(queryVO);
        queryVO.setOrgId(null);
        queryVO.setMemberId(null);
        return roleOrgService.getRoleListByOrgId(queryVO);
    }

    @ApiAnnotation(modularName = "组织机构管理", description = "绑定/解绑角色组织机构关系")
    @PostMapping("/bindRoleOrgByOrgId")
    public NCResult bindRoleOrgByRoleId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setMainSource(false);
        roleOrgService.bindRoleOrg(bindVO);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "组织机构管理", description = "查询组织机构拥有成员列表")
    @PostMapping("/getMemberListByOrgId")
    public NCResult getMemberListByOrgId(@RequestBody QueryRelationVO<UpmsOrg, UpmsMember> queryVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(queryVO);
        queryVO.setOrgId(null);
        queryVO.setMemberId(null);
        return orgMemberService.getMemberListByOrgId(queryVO);
    }

    @ApiAnnotation(modularName = "组织机构管理", description = "绑定/解绑组织机构成员关系")
    @PostMapping("/bindOrgMemberByOrgId")
    public NCResult bindOrgMemberByOrgId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setMainSource(true);
        orgMemberService.bindOrgMember(bindVO);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "组织机构管理", description = "查询组织机构树", accessSource = 2)
    @PostMapping("/getOrgList")
    public NCResult<UpmsOrg> getOrgList(@RequestBody UpmsOrg upmsOrg) {
        return NCResult.ok(orgService.getOrgList(upmsOrg));
    }
}
